package com.maozzi.web.controller.rest.front;

import com.maozzi.api.user.UserService;
import com.maozzi.api.user.model.UserDTO;
import com.maozzi.common.model.ResultVO;
import com.maozzi.common.util.CloneUtil;
import com.maozzi.common.util.ParamCheckUtil;
import com.maozzi.web.model.front.UserVO;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;

/**
 * 注册 controller
 *
 * @author maozi
 */
@RestController
@RequestMapping("/rest/front/signup")
public class SignupController {
    @Resource
    private UserService userService;

    /**
     * 用户注册
     *
     * @param vo 参数
     * @return 返回
     */
    @PostMapping("signup.json")
    public ResultVO<Boolean> signup(UserVO vo) {
        ParamCheckUtil.notNull(vo, "参数错误");
        ParamCheckUtil.notBlank(vo.getName(), "用户名为空");
        ParamCheckUtil.notBlank(vo.getPassword(), "密码为空");
        ParamCheckUtil.notBlank(vo.getConfirmPassword(), "确认密码为空");
        ParamCheckUtil.verify(vo.getConfirmPassword().equals(vo.getPassword()), "密码与确认密码不一致");

        UserDTO dto = CloneUtil.to(vo, UserDTO.class);
        userService.signup(dto);

        return ResultVO.success(true);
    }
}
